<?php


namespace Home\Controller;

//use Home\DAO\TaobaoTrades;
ini_set('max_execution_time', '0');     //设置为0，就是不限制执行的时间。
use Home\Common\FIdConst;
use Home\Service\TobaoService;
use Home\Service\UserService;

Vendor('Taobao.TopSdk');

class TaobaoorderController extends PSIBaseController
{
    private function tbkeys(){
        return array(
            array(
                'appkey' => '32974608',
                'AppSecret' => 'e10ac4e66a3efb57e71a33fe82fab787',
                'SessionKey' => '6102800e1576e8fca2606b8bdf9642858bfb30da18e10fe2209208744723' //康多力旗舰店 6102800e1576e8fca2606b8bdf9642858bfb30da18e10fe2209208744723
            ),                  //610251788e398d2ebbd1a0e041f829d3e832ca5888cb0ec2209208744723
            array(
                'appkey' => '32974608',
                'AppSecret' => 'e10ac4e66a3efb57e71a33fe82fab787',
                'SessionKey' => '61007304ab8e7b33867b3f6dea3490d7a1721610b39c13a2212036011118' //gax旗舰店
            ),
            array(
                'appkey' => '32974608',
                'AppSecret' => 'e10ac4e66a3efb57e71a33fe82fab787',
                'SessionKey' => '6101d244ef939680aa0f692b26a083f510021609d62a9662210410999593' //伊安迪旗舰店
            ),
            array(
                'appkey' => '32974608',
                'AppSecret' => 'e10ac4e66a3efb57e71a33fe82fab787',
                'SessionKey' => '6102117ec172038487d8a117e173ff5640bd572bd3fc7f72212267896990' //帝成个人护理旗舰店
            )
        );
    }
    protected function stdClass_array($array) {
        if(is_object($array)) {
            $array = (array)$array;
        }
        if(is_array($array)) {
            foreach($array as $key=>$value) {
                $array[$key] = self::stdClass_array($value);
            }
        }
        return $array;
    }
    //淘宝SDK基础配置,避免
    private function taobaosdk(){

    }
    //自定义时间拉取淘宝数据,用于更新
    protected function auth($appkey, $secretKey, $sessionKey, $startdate, $enddate)
    {
        $c = new \TopClient;
        $c->appkey = $appkey;
        $c->secretKey = $secretKey;
        $page_no = 1;
        $c->format = 'json';
        $c->simplify = 'true';
        $sessionKey = $sessionKey;
        $req = new \TradesSoldGetRequest;
        $req->setPageNo($page_no);
        $req->setPageSize(100);  //Money
        $req->setUseHasNext("true");
        $req->setFields("tid,type,status,payment,orders,receiver_mobile,receiver_phone,receiver_name,buyer_nick,has_buyer_message,o2o_guide_id,o2o_guide_name,end_time,created,pay_time,modified");
//        $req->setStartCreated("2021-07-20 00:00:00");
//        $req->setEndCreated("2021-08-06 12:00:00");
        $req->setStartCreated($startdate);
        $req->setEndCreated($enddate);
        $resp = $c->execute($req, $sessionKey);
        $data = self::stdClass_array($resp);
        while ($data['has_next'] == true){
            $req->setPageNo(++$page_no);
            echo($page_no . '_');
            $resp = $c->execute($req, $sessionKey);
            $temp = self::stdClass_array($resp);
            $data['has_next'] =$temp['has_next'];
            $data['trades']['trade'] = array_merge($data['trades']['trade'],$temp['trades']['trade']);
        }
//        var_dump($data['trades']['trade']['0']);die();
        unset($c,$req);
        return $data;
    }
    //拉取淘宝数据配置接口，主要用来新拉数据
    protected function sold($appkey, $secretKey, $sessionKey )
    {
        $c = new \TopClient;
        $c->appkey = $appkey;
        $c->secretKey = $secretKey;
        $page_no = 1;
        $c->format = 'json';
        $c->simplify = 'true';
//        $sessionKey = '610251788e398d2ebbd1a0e041f829d3e832ca5888cb0ec2209208744723';
//        $sessionKey = $sessionKey;
        $req = new \TradesSoldGetRequest;
        $req->setPageNo($page_no);
        $req->setPageSize(100);  //Money
        $req->setUseHasNext("true");
        $req->setFields("tid,type,status,payment,orders,receiver_mobile,receiver_phone,receiver_name,buyer_nick,has_buyer_message,seller_nick,o2o_guide_id,o2o_guide_name,end_time,created,pay_time,modified");
//        $req->setStartCreated("2021-07-20 00:00:00");
//        $req->setEndCreated("2021-08-06 12:00:00");
        $req->setStartCreated(date("Y-m-d H:i:s", strtotime("-15 day")));
        $req->setEndCreated(date("Y-m-d H:i:s"));
        $resp = $c->execute($req, $sessionKey);
        $data = self::stdClass_array($resp);
//        var_dump($data['trades']['trade']);

        while ($data['has_next'] == true){
            $req->setPageNo(++$page_no);
            echo($page_no . '_');
            $resp = $c->execute($req, $sessionKey);
            $temp = self::stdClass_array($resp);
            $data['has_next'] =$temp['has_next'];
//            $data['trades']['trade'][] = $temp['trades']['trade'];
            $data['trades']['trade'] = array_merge($data['trades']['trade'],$temp['trades']['trade']);
//            echo $data['has_next'];
        }
//        var_dump($data['trades']['trade']['0']);die();
        return $data;
    }
    /*更新淘宝订单数据入口*/
    public function savesold()
    {
        $appkey = '32974608';
        $secretKey = 'e10ac4e66a3efb57e71a33fe82fab787';
        $sessionKey = '6101d244ef939680aa0f692b26a083f510021609d62a9662210410999593';
        $dao = new TobaoService();
        $resp = self::sold($appkey, $secretKey, $sessionKey);
        $dao->saveorder($resp['trades']['trade']);
    }
    public function listdisplay()
    {

    }
    public function updateorder(){
        $startdate = date('Y-m-d H:i:s',strtotime('-30 day'));
        $enddate = date('Y-m-d H:i:s', strtotime('-15 day'));

        $shopskey = self::tbkeys();
        $dao = new TobaoService();
        foreach ($shopskey as $value){
            $data = self::sold($value['appkey'], $value['AppSecret'], $value['SessionKey'], $startdate, $enddate );
            $dao->updateorder($data['trades']['trade']);
        }
        echo 'success';
    }
    public function srIndex()
    {
        $us = new UserService();

        if ($us->hasPermission(FIdConst::SALE_REJECTION)) {
            $this->initVar();

            $this->assign("title", "淘宝订单列表");

            $this->assign("pAdd", $us->hasPermission(FIdConst::SALE_REJECTION_ADD) ? "1" : "0");
            $this->assign("pEdit", $us->hasPermission(FIdConst::SALE_REJECTION_EDIT) ? "1" : "0");
            $this->assign(
                "pDelete",
                $us->hasPermission(FIdConst::SALE_REJECTION_DELETE) ? "1" : "0"
            );
            $this->assign(
                "pCommit",
                $us->hasPermission(FIdConst::SALE_REJECTION_COMMIT) ? "1" : "0"
            );
            $this->assign("pGenPDF", $us->hasPermission(FIdConst::SALE_REJECTION_PDF) ? "1" : "0");
            $this->assign("pPrint", $us->hasPermission(FIdConst::SALE_REJECTION_PRINT) ? "1" : "0");

            $this->display();
        } else {
            $this->gotoLoginPage("/Home/Sale/srIndex");
        }
    }
    public function tbbillList()
    {
        if (IS_POST) {
            $params = array(
                "seller_nick" => I("post.seller_nick"),
                "tid" => I("post.tid"),
                "fromcreated" => I("post.fromcreated"),
                "tocreated" => I("post.tocreated"),
                "page" => I("post.page"),
                "start" => I("post.start"),
                "limit" => I("post.limit")
            );
            $sr = new TobaoService();
            $this->ajaxReturn($sr->tbbillList($params));
        }
    }
    public function tbshopssold()
    {
//        $shopskey = array(
//          array(
//              'appkey' => '32974608',
//              'AppSecret' => 'e10ac4e66a3efb57e71a33fe82fab787',
//              'SessionKey' => '610251788e398d2ebbd1a0e041f829d3e832ca5888cb0ec2209208744723' //康多力旗舰店
//          ),
//          array(
//              'appkey ' => '32974608',
//              'AppSecret' => 'e10ac4e66a3efb57e71a33fe82fab787',
//              'SessionKey' => '61007304ab8e7b33867b3f6dea3490d7a1721610b39c13a2212036011118' //gax旗舰店
//          )
//        );
        $shopskey = self::tbkeys();
        $dao = new TobaoService();
        foreach ($shopskey as $value){
            $data = self::sold($value['appkey'], $value['AppSecret'], $value['SessionKey']);
            if ($data['trades']['trade'] != null ) $dao->saveorder($data['trades']['trade']);
        }
    }
}